/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Invitation;
import javax.ejb.Stateless;
import java.util.List;
import entity.User;
import java.util.Collection;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class UserManager {

    @PersistenceContext
    public EntityManager em;

    public List<User> getAllUsers(){
            return em.createNamedQuery(User.FIND_ALL, User.class)
                  .getResultList();  
     }

    public void save(User user) {
        
        try{
        em.persist(user);
        }
        catch( EntityExistsException e ){      
        }  
    }

    public List<User> getUsersFind(String query){
        return em.createNamedQuery(User.FIND_BY_SEARCH, User.class)
                  .setParameter("query", query+"%").getResultList();  
    }

    public List<User> getUser(String nickname){
            return em.createNamedQuery(User.FIND_ALL, User.class)
                  .setParameter("nickname", nickname).getResultList();  
     }
    
    public boolean UpdateUser( User user ){
        User mergeUser = em.merge( user );
        mergeUser.setPublicCal( user.getPublicCal() );
        return true;
    }
}
